package com.okaixz.common.client.utils;

import com.alibaba.fastjson2.JSON;
import com.okaixz.common.client.vo.GetAccessTokenResult;
import com.okaixz.common.client.vo.WXUserInfo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;

import java.net.URLEncoder;

/**
 * @author 陈大专
 * @time 2024/10/12 11:13
 * @des 微信登录相关操作工具类
 **/
@Slf4j
@Component
public class WxLoginUtil {

//    private String redirect_uri = "https://ux328251nc8.vicp.fun/api/wx/loginCallBack";
    private String redirect_uri = "https://www.okaixz.com/api/wx/loginCallBack";
    private String appid = "wxa3c9a800aef8debc";
    private String secret = "21f79601b945d10d6e0a53d2d0b0a557";
//    private String appid = "wxd046a914c48cf521";
//    private String secret = "01ecd80ea8afd1f95cb7fb1071d96d0c";

    /**
     * 获取微信扫描二维码链接
     *
     * @return
     */
    public String getQrcode(String sid) {
        String response_type = "code";
        String scope = "snsapi_userinfo";
        StringBuilder sb = new StringBuilder();
        sb.append("https://open.weixin.qq.com/connect/oauth2/authorize?appid=");
        sb.append(appid);
        sb.append("&redirect_uri=");
        sb.append(URLEncoder.encode(redirect_uri));
        sb.append("&response_type=");
        sb.append(response_type);
        sb.append("&scope=");
        sb.append(scope);
        sb.append("&state=");
        sb.append(sid);
        sb.append("#wechat_redirect");

        return sb.toString();
    }

    /**
     * 通过code获取网页授权access_token
     *
     * @param code
     * @return
     */
    public GetAccessTokenResult getAccessToken(String code) {
        String url = "https://api.weixin.qq.com/sns/oauth2/access_token?appid=";
        String grantType = "authorization_code";
        StringBuilder sb = new StringBuilder();
        sb.append(url);
        sb.append(appid);
        sb.append("&secret=");
        sb.append(secret);
        sb.append("&code=");
        sb.append(code);
        sb.append("&grant_type=");
        sb.append(grantType);
        url = sb.toString();
        log.info(url);

        GetAccessTokenResult result = null;
        try {
            String s = HttpClientUtil.doGet(url, null);
            log.info("result={}", s);
            result = JSON.parseObject(s, GetAccessTokenResult.class);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return result;
    }

    /**
     * 刷新accessToken
     *
     * @param refreshToken
     * @return
     */
    public GetAccessTokenResult refreshAccessToken(String refreshToken) {
        String url = "https://api.weixin.qq.com/sns/oauth2/refresh_token?appid=";
        StringBuilder sb = new StringBuilder();
        sb.append(url);
        sb.append(appid);
        sb.append("&grant_type=refresh_token");
        sb.append("&refresh_token=");
        sb.append(refreshToken);
        url = sb.toString();
        log.info(url);

        GetAccessTokenResult result = null;
        try {
            String s = HttpClientUtil.doGet(url, null);
            log.info("result={}", s);
            result = JSON.parseObject(s, GetAccessTokenResult.class);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return result;
    }

    /**
     * 获取微信用户信息
     *
     * @param
     * @return
     */
    public WXUserInfo getUserInfo(String accessToken, String openid) {
        String url = "https://api.weixin.qq.com/sns/userinfo?access_token=";
        StringBuilder sb = new StringBuilder();
        sb.append(url);
        sb.append(accessToken);
        sb.append("&openid=");
        sb.append(openid);
        sb.append("&lang=zh_CN");
        url = sb.toString();
        log.info(url);

        WXUserInfo result = null;
        try {
            String s = HttpClientUtil.doGet(url, null);
            log.info("result={}", s);
            result = JSON.parseObject(s, WXUserInfo.class);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return result;
    }


}
